<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="变更单号" prop="changeNo">
        <el-input
          v-model="queryParams.changeNo"
          placeholder="请输入变更单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人id" prop="appplyUserId">
        <el-input
          v-model="queryParams.appplyUserId"
          placeholder="请输入申请人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人姓名" prop="applyUserName">
        <el-input
          v-model="queryParams.applyUserName"
          placeholder="请输入申请人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="点位id" prop="pointId">
        <el-input
          v-model="queryParams.pointId"
          placeholder="请输入点位id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变更原因" prop="changeReason">
        <el-input
          v-model="queryParams.changeReason"
          placeholder="请输入变更原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划变更时间" prop="planChangeTime">
        <el-date-picker clearable
          v-model="queryParams.planChangeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划变更时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工艺危害分析" prop="hazardAnalysis">
        <el-input
          v-model="queryParams.hazardAnalysis"
          placeholder="请输入工艺危害分析"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管控措施" prop="controlMeasure">
        <el-input
          v-model="queryParams.controlMeasure"
          placeholder="请输入管控措施"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附件ID" prop="attachmentId">
        <el-input
          v-model="queryParams.attachmentId"
          placeholder="请输入附件ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附加名称" prop="attachmentName">
        <el-input
          v-model="queryParams.attachmentName"
          placeholder="请输入附加名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附件URL" prop="attachmentUrl">
        <el-input
          v-model="queryParams.attachmentUrl"
          placeholder="请输入附件URL"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['alarm:applyChange:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['alarm:applyChange:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['alarm:applyChange:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['alarm:applyChange:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyChangeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="变更单号" align="center" prop="changeNo" />
      <el-table-column label="申请人id" align="center" prop="appplyUserId" />
      <el-table-column label="申请人姓名" align="center" prop="applyUserName" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点位id" align="center" prop="pointId" />
      <el-table-column label="变更类型: 阈值、死区、搁置、抑制" align="center" prop="changeType" />
      <el-table-column label="变更原因" align="center" prop="changeReason" />
      <el-table-column label="审核状态: 0待审核、1审核中、2通过、3驳回;   搁置直接通过，不需要审核" align="center" prop="approvalStatus" />
      <el-table-column label="计划变更时间" align="center" prop="planChangeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planChangeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工艺危害分析" align="center" prop="hazardAnalysis" />
      <el-table-column label="管控措施" align="center" prop="controlMeasure" />
      <el-table-column label="附件ID" align="center" prop="attachmentId" />
      <el-table-column label="附加名称" align="center" prop="attachmentName" />
      <el-table-column label="附件URL" align="center" prop="attachmentUrl" />
      <el-table-column label="状态:1正常 0关闭" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['alarm:applyChange:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['alarm:applyChange:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报警变更申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="变更单号" prop="changeNo">
          <el-input v-model="form.changeNo" placeholder="请输入变更单号" />
        </el-form-item>
        <el-form-item label="申请人id" prop="appplyUserId">
          <el-input v-model="form.appplyUserId" placeholder="请输入申请人id" />
        </el-form-item>
        <el-form-item label="申请人姓名" prop="applyUserName">
          <el-input v-model="form.applyUserName" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="点位id" prop="pointId">
          <el-input v-model="form.pointId" placeholder="请输入点位id" />
        </el-form-item>
        <el-form-item label="变更原因" prop="changeReason">
          <el-input v-model="form.changeReason" placeholder="请输入变更原因" />
        </el-form-item>
        <el-form-item label="计划变更时间" prop="planChangeTime">
          <el-date-picker clearable
            v-model="form.planChangeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划变更时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工艺危害分析" prop="hazardAnalysis">
          <el-input v-model="form.hazardAnalysis" placeholder="请输入工艺危害分析" />
        </el-form-item>
        <el-form-item label="管控措施" prop="controlMeasure">
          <el-input v-model="form.controlMeasure" placeholder="请输入管控措施" />
        </el-form-item>
        <el-form-item label="附件ID" prop="attachmentId">
          <el-input v-model="form.attachmentId" placeholder="请输入附件ID" />
        </el-form-item>
        <el-form-item label="附加名称" prop="attachmentName">
          <el-input v-model="form.attachmentName" placeholder="请输入附加名称" />
        </el-form-item>
        <el-form-item label="附件URL" prop="attachmentUrl">
          <el-input v-model="form.attachmentUrl" placeholder="请输入附件URL" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApplyChange, getApplyChange, delApplyChange, addApplyChange, updateApplyChange } from "@/api/alarm/applyChange"

export default {
  name: "ApplyChange",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警变更申请表格数据
      applyChangeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        changeNo: null,
        appplyUserId: null,
        applyUserName: null,
        applyTime: null,
        pointId: null,
        changeType: null,
        changeReason: null,
        approvalStatus: null,
        planChangeTime: null,
        hazardAnalysis: null,
        controlMeasure: null,
        attachmentId: null,
        attachmentName: null,
        attachmentUrl: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询报警变更申请列表 */
    getList() {
      this.loading = true
      listApplyChange(this.queryParams).then(response => {
        this.applyChangeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        changeNo: null,
        appplyUserId: null,
        applyUserName: null,
        applyTime: null,
        pointId: null,
        changeType: null,
        changeReason: null,
        approvalStatus: null,
        planChangeTime: null,
        hazardAnalysis: null,
        controlMeasure: null,
        attachmentId: null,
        attachmentName: null,
        attachmentUrl: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加报警变更申请"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getApplyChange(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改报警变更申请"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplyChange(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addApplyChange(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除报警变更申请编号为"' + ids + '"的数据项？').then(function() {
        return delApplyChange(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('alarm/applyChange/export', {
        ...this.queryParams
      }, `applyChange_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
